/***判断登录***/
var sessionId=localStorage.getItem('sid')?localStorage.getItem('sid'):''
// if(!sessionId){
//     window.location.href = '/gridding_app/page/login/login.html'
// }
var userInfo=localStorage.getItem('userInfo')?JSON.parse(localStorage.getItem('userInfo')):''
if(userInfo){
    var uid=userInfo.id
    var userid=userInfo.userid
}else{
    var uid
}
/*****获取根目录***/
function getRootPath(){
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名，如：/uimcardprj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    return(localhostPaht+projectName);
}
/***界面弹出吐司提示 ***/
function outSet(msg) { 
    console.log(msg); 
} 

function outLine(msg) { 
    mui.toast(msg); 
} 
/***接口路径***/
function httpurl(url,fromwho){
    switch (fromwho){
        case 'manage':
            return  "http://114.115.159.157:8081/grid/public/index.php/manage/"+url+'?device=app';
            break;
        case 'user':
            return "http://114.115.159.157:8081/grid/public/index.php/manage/"+url+'?device=app';
            break;
        default:
return  "http://114.115.159.157:8081/grid/public/index.php/company/"+url+'?device=app'
            break;
    }
}
/***裁剪字符串****/
function cutstring(str,len){
    if(str.length<len){
        var len=str.length
        var newstr=str.substring(0,len)
    }else{
        var newstr=str.substring(0,len)+'...'
    }
    return newstr
}
/***json转字符串****/
function jsonToString(obj){
    return JSON.stringify(obj)
}
function changetempname(array){
    var newone={}
    var getDic={
        fromwho:'ld', 
        url:'Dictionary/listDictionary',
        sendData:{
            tag:'all'
        }
    }
    var reg=/\{\{(.+?)\}\}/
    getAjaxSyn(getDic,function(data){
//      console.log(data.data)
        $.each(data.data,function(k,v){
            var one=reg.exec(v.tag)
            if(one){
                if($.inArray(one[1], array)!=-1){
                    newone[one[1]]=v.name
                }
            }
        })
    },function(){})
    return newone
}
/***字符串转html****/
function decodeHtml(s) {
    var HTML_DECODE = {
        "&lt;": "<",
        "&gt;": ">",
        "&amp;": "&",
        "&nbsp;": " ",
        "&quot;": "\"",
        "&copy;": ""

        // Add more
    };

    var REGX_HTML_ENCODE = /"|&|'|<|>|[\x00-\x20]|[\x7F-\xFF]|[\u0100-\u2700]/g;

    var REGX_HTML_DECODE = /&\w+;|&#(\d+);/g;

    var REGX_TRIM = /(^\s*)|(\s*$)/g;

    s = (s != undefined) ? s : "";
    return (typeof s != "string") ? s :
        s.replace(REGX_HTML_DECODE,
                  function ($0, $1) {
                      var c = HTML_DECODE[$0];
                      if (c == undefined) {
                          // Maybe is Entity Number
                          if (!isNaN($1)) {
                              c = String.fromCharCode(($1 == 160) ? 32 : $1);
                          } else {
                              c = $0;
                          }
                      }
                      return c;
                  });
};
/***原生加载中****/
//function loadingtoggle(type){
//  if(plus){
//      if(type=='show'){
//          plus.nativeUI.showWaiting(); 
//      }else{
//          plus.nativeUI.closeWaiting();
//      }
//  }
//}
/**倒计时****/
function resetCode(element,second,callback){
    var secondString=second+'s后重发'
    $(element).html(secondString);
    var timer = null;
    timer = setInterval(function(){
        second -= 1;
        if(second >=0 ){
            secondString=second+'s后重发'
            $(element).html(secondString);
        }else{
            clearInterval(timer);
            callback();
        }
    },1000);
}
/**获取表单数据****/
function getformData(formelement){
    var data={}
    $(formelement).find('input,textarea,select').each(function(k,v){
        var vtype=$(v).attr('validate')
        var vtitle=$(v).attr('title')
//      console.log(vtitle)
//      switch (vtype){
//      	case 'require':
//      	   alert(vtitle+'不能为空')
//             return false;   
//      	   break;
//      	default:
//      		break;
//      }
//      //多选
//      //下拉框
//      //文本
        data[$(v).attr('name')]=$(v).val()
    })
    return data
}


/***文件路径****/
function catchurl(url,type){
    if(!url){
        return false;
    }
    if(url.indexOf('http:')==-1){
        if(type=='ld'){
            
        }
        return  "http://114.115.159.157:8081/grid/public/upload/issue/"+url;
    }else{
        return url
    }
}
/***获取get传参****/
function GetQueryString (name){
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null){
//      return  unescape(r[2])
        return decodeURIComponent(r[2])
    };
    return null;
}
/***时间格式转换****/
function timeDate(time,type,language){
    
    const date = new Date(time); //获取一个时间对象
    var year=date.getFullYear();  // 获取完整的年份(4位,1970)
    var month=date.getMonth()+1;  // 获取月份(0-11,0代表1月,用的时候记得加上1)
    var day=date.getDate();  // 获取日(1-31)
    var hour=date.getHours();  // 获取小时数(0-23)
    if(hour<10){
        hour='0'+hour
    }
    var min=date.getMinutes();  // 获取分钟数(0-59)
    if(min<10){
        min='0'+min
    }
    var sec=date.getSeconds();  // 获取秒数(0-59)
    if(sec<10){
        sec='0'+sec
    }
    var ms=date.getTime();  // 获取时间(从1970.1.1开始的毫秒数)
    if(language){
        var date_ymd_ch=year + "年" + month + "月" + day + "日"
        return date_ymd_ch
    }
    if(!type){
        var date_ymd=year + "-" + month + "-" + day
        return date_ymd
    }else{
        var date_hms=year + "-" + month + "-" + day+'  '+hour+":"+min+":"+sec
        return date_hms
    }
    
}
/***拆分字符串****/
function splitString(string,spe,index){
    var spe=spe ?spe :''
    var index=index ? index:0
    var arr=string.split(spe)
    return arr[index]
}
/***时间差****/
function timeBetween(time){
    var nms=new Date().getTime();
    var bms=nms-time;
    var rtime='';
    var month= Math.floor(bms / (1000 * 60 * 60 * 24 * 30));
    var d= Math.floor(bms / (1000 * 60 * 60 * 24));
    var h= Math.floor(bms / (1000 * 60 * 60));
    var m = Math.floor(bms / (1000 * 60));
    var s = Math.ceil(bms / (1000));
    if(month>0){rtime= month +'月前';}else{
        if(d>0){rtime= d +'天前';}else{
            if(h>0){rtime= h +'小时前';}else{
                if(m>0){rtime= m +'分钟前';    }else{
                    rtime= s + '秒前';
                }
            }
        }
    }
    if(month>12){
        rtime='1年前及更早';
    }
    return rtime;
}
/***base64位图片转文件流****/
function convertBase64UrlToBlob(urlData){  
    if(urlData){
        var bytes=window.atob(urlData.split(',')[1]);
        var ab = new ArrayBuffer(bytes.length);  
        var ia = new Uint8Array(ab);  
        for (var i = 0; i < bytes.length; i++) {  
            ia[i] = bytes.charCodeAt(i);  
        }  
        return new Blob( [ab] , {type : 'image/png'}); 
    }
}
/**************ajax方法*************/
//获取数据
function getAjax(data,successCallback,beforeCallback,completeCallback){
    var fromwho=data.fromwho
    var url=httpurl(data.url,fromwho)
    var sendData=data.sendData ? data.sendData :''
    $.ajax({
        type:"get",
        url:url,
//      dataType:'json',
        data:sendData,
        beforeSend: function(request) {
            try{ beforeCallback()}catch(e){}
        },
        success:function(data){
			successCallback(data)
        },
        complete:function(){
            try{completeCallback()}catch(e){}
        },
        error:function(data){
            console.log('服务器出错')
        }
    });
}
function getAjaxSyn(data,successCallback,beforeCallback,completeCallback){
    var fromwho=data.fromwho
    var url=httpurl(data.url,fromwho)
    var sendData=data.sendData ? data.sendData :''
    $.ajax({
        type:"get",
        url:url,
        dataType:'json',
        data:sendData,
        async:false,
        timeout : 4000,
        beforeSend: function(request) {
            try{ beforeCallback()}catch(e){}
        },
        success:function(data){
            successCallback(data)
        },
        complete:function(){
             try{completeCallback()}catch(e){}
        },
        error:function(data){
            console.log('服务器出错')
        }
    });
}
//提交数据
function upAjax(data,successCallback,beforeCallback,completeCallback){
    var fromwho=data.fromwho
    var url=httpurl(data.url,fromwho)
    var sendData=data.sendData ? data.sendData :''
    $.ajax({
        type:"post",
        url:url,
        dataType:'json',
        data:sendData,
        beforeSend: function(request) {
            try{ beforeCallback()}catch(e){}
        },
        success:function(data){
            successCallback(data)
        },
        complete:function(){
              try{completeCallback()}catch(e){}
        },
        error:function(data){
            console.log('服务器出错')
        }
    });
}
//提交流数据
function upStreamAjax(data,formdata,successCallback,beforeCallback,completeCallback){
    var fromwho=data.fromwho
    var url=httpurl(data.url,fromwho)
    $.ajax({
        type:"post",
        url:url,
        data:formdata,
        dataType:'json',
        cache:false,
        processData:false,
        contentType:false,
        beforeSend: function(request) {
             try{ beforeCallback()}catch(e){}
        },
        success:function(data){
            successCallback(data)
        },
        complete:function(){
            try{completeCallback()}catch(e){}

        },
        error:function(data){
            console.log('服务器出错')
        }
    });
}
